#include <iostream>
#include <vector>

/*
 * you want to represent part-whole hierarchies of objects
 * you want clients to be able to ignore the difference between compositions of objects and individual objects
 */

class Component
{
public:
    virtual ~Component() {}

    virtual Component *getChild(const unsigned int)
    {
        return 0;
    }

    virtual void add(Component *) {}
    virtual void remove(const unsigned int) {}
    virtual void operation() = 0;
};

class Composite : public Component
{
public:
    ~Composite()
    {
        for (unsigned int i = 0; i < children_.size(); i++)
        {
            delete children_[i];
        }
    }

    Component *getChild(const unsigned int index)
    {
        return children_[index];
    }

    void add(Component *component)
    {
        children_.push_back(component);
    }

    void remove(const unsigned int index)
    {
        Component *child = children_[index];
        children_.erase(children_.begin() + index);

        delete child;
    }

    void operation()
    {
        for (unsigned int i = 0; i < children_.size(); i++)
        {
            children_[i]->operation();
        }
    }

private:
    std::vector<Component *> children_;
};

class Leaf : public Component
{
public:
    Leaf(const int i) : id_(i) {}
    ~Leaf() {}

    void operation()
    {
        std::cout << "Leaf " << id_ << " operation" << std::endl;
    }

private:
    int id_;
};

int main()
{
    Composite composite;

    for (unsigned int i = 1; i <= 5; i++)
    {
        composite.add(new Leaf(i));
    }

    composite.remove(0);

    composite.operation();

    return 0;
}